package com.hongsheng.service.impl;/*
@author:刘逸思普
@Date:2023/8/10
 */

//import com.github.pagehelper.PageHelper;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hongsheng.mapper.*;
import com.hongsheng.pojo.*;
import com.hongsheng.pojo.vo.OrderReportVo;
import com.hongsheng.service.OrderService;
import com.hongsheng.util.NewGetDistance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl  implements OrderService {
    private static final double CUSP=1;
    private static final double OP=10;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private LineMapper lineMapper;
    @Autowired
    private BillMapper billMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private DriverMapper driverMapper;
    @Override
    public List<Order> selectOrders() {

        return orderMapper.findOrderList();
    }
    @Override
    public Boolean setAmount(Order order){
        Double amount=0.0;
        Double trainNum=0.0;
        for (Goods goods : order.getGoodsList()) {
            Double packNum = goods.getSize() * goods.getQuantity() * goods.getPack().getPrice();
            amount+=packNum;
            Double deNum= goods.getSize() * goods.getQuantity() *CUSP;
            amount+=deNum;
        }

        Double totalOrderSize = orderMapper.totalOrderSize(order.getId());
        Double totalOrderWeight = orderMapper.totalOrderWeight(order.getId());
        if (totalOrderWeight==null){
            totalOrderWeight =0.0;
        }
        if (totalOrderSize==null){
            totalOrderSize=0.0;
        }
        Double z=totalOrderSize/totalOrderWeight;

        if (z<=0.01) {
            trainNum=2.0;
        } else if (z>0.01&&z <= 1.0) {
            trainNum = 0.5;
        } else if (z > 1.0 && z <= 1.5) {
            trainNum = 1.0;
        } else if (z > 1.5 && z <= 2.0) {
            trainNum = 1.5;
        } else if (z > 2.0) {
            trainNum = 2.0;
        }
        Double v = trainNum * order.getLine().getDistance();
        amount+=v;
        Integer integer = orderMapper.setAmount(amount,order.getId());
return integer>0;
    }


    //分页查询，条件查询
    @Override
    public PageInfo<Order> selectOrdersByLimit(Integer currentPage, Integer pageSize, Order order) {
        if (order.getLine().getStartAddress()!=null){
            String s=order.getLine().getStartAddress();
            order.getLine().setStartAddress("%"+s+"%");
        }
        if (order.getCustomer().getName()!=null){
            String s=order.getCustomer().getName();
            order.getCustomer().setName("%"+s+"%");
        }
        PageHelper.startPage(currentPage,pageSize);
        if (order.getCreateTime()!=null){
            String format = new SimpleDateFormat("yyyy-MM-dd").format(order.getCreateTime());
            order.setCreateTime(java.sql.Date.valueOf(format));
        }

        List<Order> orderList = orderMapper.findOrderPage(order);
        return  new PageInfo<Order>(orderList);
//        return orderList;
    }

    @Override
    public Boolean addOrder(Order order) {
        Line line = order.getLine();
        line.setDistance(NewGetDistance.distance(line.getStartAddress(), line.getEndAddress()));
        Integer aBoolean = lineMapper.insert(line);
        if(aBoolean>0){
            order.setLine(line);
        }
        System.out.println(line.getId());
        if (order.getOrderStatus()==null){
            order.setOrderStatus(0);
        }
        return orderMapper.addOrder(order)>0;
    }

    @Override
    public List<Driver> findDrivers(Order order) {
        List<Driver> drivers = driverMapper.findFreeDriver();
        List<Driver> driverList = orderMapper.findDriverByOid(order.getId());
        List<Driver> array=new ArrayList<>();
        for (int i = 0; i < drivers.size(); i++) {
            if(drivers.get(i).getAddress()==null){
                drivers.get(i).setAddress(order.getLine().getStartAddress());
            }
            if (drivers.get(i).getAddress().equals(order.getLine().getStartAddress())){
               array.add(drivers.get(i));
            }
        }
        System.out.println(drivers);
        array.addAll(driverList);
        System.out.println(array);
      return array;
    }

    @Override
    public Boolean updateOrder(Order order) {
        return orderMapper.updateOrder(order)>0;
    }

    @Override
    public Boolean deleteOrder(Integer id) {
        Integer integer = orderMapper.delGoodsByOid(id);

        return integer>0&&orderMapper.deleteOrder(id)>0;
    }

    @Override
    public Order findOrderById(Integer id) {
        return orderMapper.findOrderById(id);
    }
    //剩余仓库容量
    @Override
    public Double findCapacity(Integer id){
        Double capacity = orderMapper.capacity(id);
        return capacity;
    }
    //车辆现有货物体积
    @Override
    public Double totalSize(Integer id){
        Double totalSize = orderMapper.totalSize(id);
        if (totalSize==null){
            totalSize=0.0;
        }
        return totalSize;
    }
    //车辆现有重量
    @Override
    public Double totalWeight(Integer id){
        Double totalWeight = orderMapper.totalWeight(id);
        if (totalWeight==null){
            totalWeight=0.0;
        }
        return totalWeight;
    }

//司机状态改变
    @Override
    public Boolean driverStu(){
        Integer integer = orderMapper.driverStu();
        return integer>0;
    }

@Override
    public Boolean getBillOut(Order order){
    Double amount = order.getAmount();
    Double cost=0.0;
    cost+=amount*0.3;
    if (order.getGoodsList()!=null){
        for (Goods goods : order.getGoodsList()) {
            Double cost1 = goodsMapper.getCost(goods.getId());
            cost+=cost1;
        }
    }
    Bill bill=new Bill();
    bill.setAmount(cost);
    bill.setType("支出");
    bill.setBillDesc("支出为订单"+order.getId());
    bill.setCreateTime(new Date());
    int i = billMapper.insertBill(bill);
    return i>0;
}

    @Override
    public Boolean billIn(Order order) {
        Bill bill=new Bill();
        bill.setAmount(order.getAmount());
        bill.setType("收入");
        bill.setBillDesc("收入为订单"+order.getId());
        bill.setCreateTime(new Date());
        int i = billMapper.insertBill(bill);
        return i>0;
    }

    @Override
    public List<String> findStartAddress() {

        return orderMapper.findStartAddress();
    }

    /***
     *  根据月份查询订单数量
     * @return List<OrderReportVo>
     * @Auth ggb
     */
    @Override
    public List<OrderReportVo> selectOrderByMonth() {
        return orderMapper.selectOrderByMonth();
    }

    @Override
    public Integer getOrderCount() {
        return orderMapper.getOrderCount();
    }

    @Override
    public Double getBillIn() {
        return orderMapper.getBillIn();
    }

    @Override
    public Integer getCustomer() {
        return orderMapper.getCustomer();
    }

    @Override
    public Double getBillOut() {
        return orderMapper.getBillOut();
    }
}
